package com.cnpinxi.util;

import java.util.concurrent.atomic.AtomicInteger;

public class GenerateID {

	private static AtomicInteger offset = new AtomicInteger(0);
	private static final Integer iii = 1000;
	private static Long base = System.currentTimeMillis() * iii;

	public static Long CreatetID() {

		if (offset.get() == iii) {
			offset.set(0);
			synchronized (GenerateID.class) {
				base = System.currentTimeMillis() * iii;
			}
		}
		Long timetemp = base + offset.incrementAndGet();
		return timetemp;

	}


	public final static Boolean IS_ONLY_NUMBER=true;
	public final static Boolean IS_NOT_ONLY_NUMBER=false;

	/**
	 * 获得随机字符串
	 * @param num  字符串长度
	 * @param isOnlyNumber 是否纯数字
	 * @return
	 */
	public static String getRandomNum(int num,Boolean isOnlyNumber) {
		// 字符源，可以根据需要删减
		  String _generateSource = "0123456789abcdefghigklmnopqrstuvwxyz";
		//String generateSource = "0123456789";
		if (isOnlyNumber){
			_generateSource = "0123456789";
		}
		StringBuffer rtnStr = new StringBuffer();
		for (int i = 0; i < num; i++) {
			// 循环随机获得当次字符，并移走选出的字符
			String generateSource=_generateSource;
			if(i==0){
				generateSource=_generateSource.substring(1,_generateSource.length());
			}
			String nowStr = String.valueOf(generateSource.charAt((int) Math.floor(Math.random()
					* generateSource.length())));
			rtnStr.append(nowStr);
			generateSource = generateSource.replaceAll(nowStr, "");
		}

		return rtnStr.toString();

	}

	public static String getRandom(int num,Boolean isOnlyNumber, String generateSource) {
		// 字符源，可以根据需要删减
//		String generateSource = "0123456789abcdefghigklmnopqrstuvwxyz";
		//String generateSource = "0123456789";
		if (isOnlyNumber){
			generateSource = "0123456789";
		}
		String rtnStr = "";
		for (int i = 0; i < num; i++) {
			// 循环随机获得当次字符，并移走选出的字符
			String nowStr = String.valueOf(generateSource.charAt((int) Math.floor(Math.random()
					* generateSource.length())));
			rtnStr += nowStr;
			generateSource = generateSource.replaceAll(nowStr, "");
		}


		return rtnStr;

	}

	public static void main(String[] a) {
/*		String textValue = "232143.dfdffds";
		// Pattern p = Pattern.compile("^//d+(//.//d+)?$");

		Pattern p = Pattern.compile("^[0-9]*$|^[0-9,.]*$");
		Matcher matcher = p.matcher(textValue);
	//	System.out.println(textValue + "====>>>>" + matcher.matches());
		for (int i=0;i<10;i++){
			System.out.println("====>>>>" + getRandomNum(10,true));
		}*/

		//String textValue = "50022519910627071x";
		/*String textValue = "510219198008063119";
		Pattern p = Pattern.compile("/^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$/; ");
		Matcher matcher = p.matcher(textValue);
		System.out.println(textValue + "====>>>>" + matcher.matches());*/
		System.out.println(getRandomNum(4,true));
	}
}
